package com.dingding.service.share.biz.impl;

import java.util.Date;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.base.daox.IBaseDaoX;
import com.dingding.common.core.beans.UserLogin;
import com.dingding.common.core.beans.share.ActivityShare;
import com.dingding.common.core.beans.share.ActivityShareRefGift;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.facade.user.service.IUserInfoFacade;
import com.dingding.facde.share.dto.CheckIsShareResult;
import com.dingding.facde.share.dto.RegActivityResult;
import com.dingding.facde.share.dto.StartShareResult;
import com.dingding.facde.share.statics.ShareActivityStatics;
import com.dingding.service.share.biz.IShareActivityService;
import com.dingding.service.share.dao.IActivityShareDao;

/**
 * @Description:分享活动service
 * @author fengshuonan
 * @date 2016年9月5日 下午5:13:52
 * @version 叮叮搭车
 * @since 2.4
 */
@Deprecated
@Component("shareActivityService")
public class ShareActivityServiceImpl implements IShareActivityService {

	private Logger log = Logger.getLogger(this.getClass());
	
	@Resource(name = "activityShareDao")
	private IActivityShareDao activityShareDao;
	
	@Resource(name = "baseDaoX")
	private IBaseDaoX baseDaoX;
	
	@Resource(name = "userInfoFacade")
	private IUserInfoFacade userInfoFacade;
	
	/**
	 * @Description:获取某个用户是否开启了某个分享活动
	 * @param userId 用户id
	 * @param activityId 活动id
	 * @author fengshuonan
	 * @return CheckIsShareResult 结果的封装
	 * @since 2.4
	 */
	@Override
	@Transactional(readOnly = true)
	public CheckIsShareResult checkIsShare(String userId, String activityId) {
		
		CheckIsShareResult checkIsShareResult = new CheckIsShareResult();
		
		//获取用户的活动分享记录
		ActivityShare activityShare = this.activityShareDao.getUserActivityShare(userId,activityId);
		
		//如果用户还没有发起该活动
		if(activityShare == null){
			checkIsShareResult.setHaveTakePart(false);
			return checkIsShareResult;
			
		//如果发起过该活动
		}else{
			checkIsShareResult.setHaveTakePart(true);
			checkIsShareResult.setInviteNum(activityShare.getInviteNum());
			return checkIsShareResult;
		}
	}
	
	/**
	 * @Description:开起活动分享
	 * @param activityId 活动id
	 * @param gifts 活动所选的礼品
	 * @author fengshuonan 
	 * @return StartShareResult
	 * @since 2.4
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public StartShareResult startShare(String activityId,String userId,String[] gifts) {
		
		StartShareResult startShareResult = new StartShareResult();
		
		//检查是否发起过分享活动
		CheckIsShareResult checkIsShare = this.checkIsShare(userId, activityId);
		if(checkIsShare.isHaveTakePart()){
			startShareResult.setActivityUrl(checkIsShare.getShareUrl());
			startShareResult.setActivityTitle(ShareActivityStatics.MOON_ACTIVITY_SHARE_TITLE);
			return startShareResult;
		}
		
		//初始化ActivityShare
		ActivityShare activityShare = new ActivityShare();
		activityShare.setAsId(ToolUtil.getUid());	//设置主键id
		activityShare.setAcId(activityId);   		//设置活动主键id
		activityShare.setInviteNum(0);  			//设置邀请人数
		activityShare.setJoinTime(new Date()); 		//设置创建分享的时间
		activityShare.setUserId(userId);			//设置用户id
		
		//添加活动分享的赠品
		for(String gift : gifts){
			ActivityShareRefGift activityShareRefGift = new ActivityShareRefGift();
			activityShareRefGift.setUgId(ToolUtil.getUid());
			activityShareRefGift.setGiftId(gift);
			try {
				this.baseDaoX.save(activityShareRefGift);
			} catch (Exception e) {
				log.error("添加赠品出错！ShareActivityServiceImpl ： startShare()",e);
				return null;
			}
		}
		
		//添加活动分享记录
		try {
			this.baseDaoX.save(activityShare);
		} catch (Exception e) {
			log.error("添加活动分享记录出错！ShareActivityServiceImpl ： startShare()",e);
			return null;
		}
		
		//封装返回结果
		startShareResult.setActivityTitle(ShareActivityStatics.MOON_ACTIVITY_SHARE_TITLE);
		
		return startShareResult;
	}

	/**
	 * @Description:针对于活动的用户注册
	 * @author fengshuonan
	 * @return RegActivityResult
	 * @since 2.4
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public RegActivityResult regUser4Activity(String activityId,String telephone, String messageCode, String shareKey, String openId) {
		
		RegActivityResult regActivityResult = new RegActivityResult();
		
		//判断验证码是否正确
		boolean codeFlag = this.activityShareDao.isMsgCodeTrue(telephone,messageCode);
		if(!codeFlag){
			regActivityResult.setMessageCodeTrue(false);
			return regActivityResult;
		}else{
			regActivityResult.setMessageCodeTrue(true);
		}
		
		//判断该手机号是否注册过
		String isPhoneReg = this.activityShareDao.isUserHaveReg(telephone);
		
		//如果手机号没注册过
		if(isPhoneReg == null){
			
			UserLogin addUser = this.userInfoFacade.addUser(telephone);
			//添加用户
			if(addUser == null){
				return null;
			}
			//增加活动发起者的邀请次数
			this.activityShareDao.addActAnsInviteNum(shareKey);
			regActivityResult.setUserId(addUser.getUid());
			regActivityResult.setStatus(1);		//1:新用户  2：老用户（没发起过活动） 3：老用户（发起过活动）
			return regActivityResult;
		
		//如果手机号注册过
		}else{
			String userId = this.activityShareDao.getUserIdByPhone(telephone);
			
			//判断是否为活动的发起者
			CheckIsShareResult checkIsShare = this.checkIsShare(userId, activityId);
			
			//如果是活动发起者，则显示邀请数量
			if(checkIsShare.isHaveTakePart()){
				regActivityResult.setUserId(userId);
				regActivityResult.setInviteNum(checkIsShare.getInviteNum());
				regActivityResult.setStatus(3);	//1:新用户  2：老用户（没发起过活动） 3：老用户（发起过活动）
				regActivityResult.setGifts(checkIsShare.getGifts());	//设置赠品
				return regActivityResult;
				
			//如果不是活动发起者，则提示您是老用户	
			}else{
				regActivityResult.setUserId(userId);
				regActivityResult.setStatus(2);//1:新用户  2：老用户（没发起过活动） 3：老用户（发起过活动）
				return regActivityResult;
			}
		}
	}
	
}
